package com.ziyun.erp.web.modules.material.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.material.entity.ErpStockEntity;
import com.ziyun.erp.modules.material.service.ErpStockService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 紫云仓库表
 *
 * @author guizhao
 * @email ...
 * @date 2018-08-13 14:27:45
 */
@RestController
@RequestMapping("material/erpstock")
public class ErpStockController {
    @Autowired
    private ErpStockService erpStockService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("material:erpstock:list")
    public List<ErpStockEntity> list(){
        List<ErpStockEntity> stockList = erpStockService.selectList(new EntityWrapper<ErpStockEntity>().eq("`delete`",0));
        for(ErpStockEntity erpStockEntity : stockList){
            ErpStockEntity parentErpStockEntity = erpStockService.selectById(erpStockEntity.getParentId());
            if(parentErpStockEntity != null){
                erpStockEntity.setParentName(parentErpStockEntity.getStockName());
            }
        }

        return stockList;
    }

    /**
     * 选择仓库(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("material:erpstock:select")
    public R select(){
        //查询列表数据
        List<ErpStockEntity> stockList = erpStockService.selectList(new EntityWrapper<ErpStockEntity>().eq("`delete`",0));

        //添加顶级菜单
        ErpStockEntity root = new ErpStockEntity();
        root.setStockId(0);
        root.setStockName("顶级仓库");
        root.setParentId(-1);
        root.setOpen(true);
        stockList.add(root);

        return R.ok().put("stockList", stockList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{stockId}")
    @RequiresPermissions("material:erpstock:info")
    public R info(@PathVariable("stockId") Integer stockId){
        ErpStockEntity erpStock = erpStockService.selectById(stockId);

        return R.ok().put("erpStock", erpStock);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("material:erpstock:save")
    public R save(@RequestBody ErpStockEntity erpStock){
        erpStock.setCreateName(""+UserUtils.getCurentUser().getUserId());
        erpStockService.insert(erpStock);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("material:erpstock:update")
    public R update(@RequestBody ErpStockEntity erpStock){
        ValidatorUtils.validateEntity(erpStock);
        erpStock.setUpdateName(""+UserUtils.getCurentUser().getUserId());
        erpStockService.updateAllColumnById(erpStock);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("material:erpstock:delete")
    public R delete(@RequestBody Integer stockId){
        System.out.println(stockId);
        erpStockService.deleteUpdate(stockId,UserUtils.getCurentUser().getUserId());

        return R.ok();
    }

}
